package dhbw.GUI.Window;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.*;

import com.jgoodies.forms.factories.Borders;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

import dhbw.Data.Language;
import dhbw.Data.Settings;
import dhbw.GUI.Menu.MyMenubar;
import dhbw.GUI.Panels.PanelDozent;
import dhbw.GUI.Panels.PanelProfessor;
import dhbw.GUI.Panels.PanelSTGL;
import dhbw.GUI.Panels.PanelStudent;
import dhbw.Roles.Dozent;
import dhbw.Roles.Professor;
import dhbw.Roles.Student;
import dhbw.Roles.Studiengangsleiter;
import Main.MyController;

public class Window extends JFrame{

	private JPanel contentpanel;
	private JMenuBar menubar;
	private JPanel bottompanel;
	private JLabel angemeldetLabel;
	private boolean debug;
	private MyController myc;

	public Window(MyController myc, boolean debug) {
		this.debug = debug;
		this.myc = myc;
		buildStandardWindow();
		this.addWindowListener(new MyWindowListener(this.myc));		
	}

	/**
	 * Build the Window-Frame
	 */
	private void buildStandardWindow() {
		this.setBackground(Settings.BG_COLOR);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setBounds(0, 0, Settings.width, Settings.height);
		this.setIconImage(Settings.LOGO_ICON.getImage());
		this.setResizable(false);
		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		this.setTitle(Settings.APPLICATION_NAME);
		this.setGUI();
		this.setVisible(true);
		myc.centerJFrame(this);
	}

	/**
	 * Generate all Components for Frame
	 */
	private void setGUI() {
		this.setLayout(new BorderLayout());
		JPanel north = new JPanel();
		north.setLayout(new BorderLayout());
		JPanel logobar = new JPanel(new BorderLayout());
		logobar.setBackground(Settings.BG_COLOR);
		north.setBorder(BorderFactory.createMatteBorder(0, 0, 3, 0, UIManager.getColor("TabbedPane.selected")));
		CellConstraints cc = new CellConstraints();
		String cols = "16dlu,pref,6dlu,pref,270dlu,pref,16dlu";
		String rows = "1dlu,25dlu,1dlu";
		FormLayout grid = new FormLayout(cols, rows);
		logobar.setLayout(grid);
		JPanel anmeldenUhr = new JPanel();
		anmeldenUhr.setLayout(new GridLayout(2, 1));
		anmeldenUhr.setBackground(Settings.BG_COLOR);
		angemeldetLabel = new JLabel(Language.LOGGEDINAS[this.myc.getLanguage()]+myc.getActiveUser().getVorname()+" "+myc.getActiveUser().getName());
		anmeldenUhr.add(angemeldetLabel);
		anmeldenUhr.add(this.myc.getTimeLabel());
		logobar.add(new JLabel(Settings.BreitesLOGO_ICON),cc.xy(6, 2));
		
		logobar.add(anmeldenUhr,cc.xy(2, 2));
		north.add("South",logobar);

		if (myc.getActiveUser().getClass().equals(Student.class)) {
			north.add("North",new MyMenubar(this.myc, "Student"));
			this.add("North", north);
			this.add("Center", new PanelStudent(this.myc));
		}
		if (myc.getActiveUser().getClass().equals(Dozent.class)) {
			north.add("North",new MyMenubar(this.myc, "Dozent"));
			this.add("North", north);
			this.add("Center", new PanelDozent(this.myc));
		}
		if (myc.getActiveUser().getClass().equals(Professor.class)) {
			north.add("North",new MyMenubar(this.myc, "Professor"));
			this.add("North", north);
			this.add("Center", new PanelProfessor(this.myc));
		}
		if (myc.getActiveUser().getClass().equals(Studiengangsleiter.class)) {
			north.add("North",new MyMenubar(this.myc, "Studiengangsleiter"));
			this.add("North", north);
			this.add("Center", new PanelSTGL(this.myc));
		}
	}
}
